using Microsoft.AspNetCore.Mvc;
using Beauty.Net.RabbitMQ.SimpleQueue;
using Beauty.Net.RabbitMQ.WorkQueue;
using Beauty.Net.RabbitMQ.PublishSubscribe;
using Beauty.Net.RabbitMQ.Routing;
using Beauty.Net.RabbitMQ.Topic;
using Beauty.Net.RabbitMQ.Headers;

namespace Beauty.NetWebApi.Controllers
{
    /// <summary>
    /// RabbitMQ消息模式演示控制器
    /// </summary>
    [ApiController]
    [Route("api/[controller]")]
    public class RabbitMQDemoController : ControllerBase
    {
        /// <summary>
        /// 简单队列生产者
        /// </summary>
        private readonly SimpleQueueProducer _simpleQueueProducer;
        
        /// <summary>
        /// 工作队列生产者
        /// </summary>
        private readonly WorkQueueProducer _workQueueProducer;
        
        /// <summary>
        /// 发布/订阅模式生产者
        /// </summary>
        private readonly PublishSubscribeProducer _publishSubscribeProducer;
        
        /// <summary>
        /// 路由模式生产者
        /// </summary>
        private readonly RoutingProducer _routingProducer;
        
        /// <summary>
        /// 主题模式生产者
        /// </summary>
        private readonly TopicProducer _topicProducer;
        
        /// <summary>
        /// 头部交换模式生产者
        /// </summary>
        private readonly HeadersProducer _headersProducer;

        /// <summary>
        /// 构造函数，注入各种RabbitMQ生产者
        /// </summary>
        /// <param name="simpleQueueProducer">简单队列生产者</param>
        /// <param name="workQueueProducer">工作队列生产者</param>
        /// <param name="publishSubscribeProducer">发布/订阅模式生产者</param>
        /// <param name="routingProducer">路由模式生产者</param>
        /// <param name="topicProducer">主题模式生产者</param>
        /// <param name="headersProducer">头部交换模式生产者</param>
        public RabbitMQDemoController(
            SimpleQueueProducer simpleQueueProducer,
            WorkQueueProducer workQueueProducer,
            PublishSubscribeProducer publishSubscribeProducer,
            RoutingProducer routingProducer,
            TopicProducer topicProducer,
            HeadersProducer headersProducer)
        {
            _simpleQueueProducer = simpleQueueProducer;
            _workQueueProducer = workQueueProducer;
            _publishSubscribeProducer = publishSubscribeProducer;
            _routingProducer = routingProducer;
            _topicProducer = topicProducer;
            _headersProducer = headersProducer;
        }

        /// <summary>
        /// 发送简单队列消息
        /// </summary>
        [HttpPost("simple-queue")]
        public IActionResult SendSimpleQueueMessage([FromBody] string message)
        {
            _simpleQueueProducer.Send("demo_simple_queue", message);
            return Ok(new { Message = "消息已发送到简单队列" });
        }

        /// <summary>
        /// 发送工作队列任务
        /// </summary>
        [HttpPost("work-queue")]
        public IActionResult SendWorkQueueTask([FromBody] string task)
        {
            _workQueueProducer.SendTask("demo_work_queue", task);
            return Ok(new { Message = "任务已发送到工作队列" });
        }

        /// <summary>
        /// 发布消息（发布/订阅模式）
        /// </summary>
        [HttpPost("publish-subscribe")]
        public IActionResult PublishMessage([FromBody] string message)
        {
            _publishSubscribeProducer.Publish("demo_fanout_exchange", message);
            return Ok(new { Message = "消息已发布到扇型交换机" });
        }

        /// <summary>
        /// 发送路由消息
        /// </summary>
        [HttpPost("routing")]
        public IActionResult SendRoutingMessage([FromQuery] string routingKey, [FromBody] string message)
        {
            _routingProducer.Send("demo_direct_exchange", routingKey, message);
            return Ok(new { Message = $"消息已发送到直接交换机，路由键：{routingKey}" });
        }

        /// <summary>
        /// 发送主题消息
        /// </summary>
        [HttpPost("topic")]
        public IActionResult SendTopicMessage([FromQuery] string topic, [FromBody] string message)
        {
            _topicProducer.Publish("demo_topic_exchange", topic, message);
            return Ok(new { Message = $"消息已发送到主题交换机，主题：{topic}" });
        }

        /// <summary>
        /// 发送头部消息
        /// </summary>
        [HttpPost("headers")]
        public IActionResult SendHeadersMessage([FromBody] string message)
        {
            var headers = new Dictionary<string, object>
            {
                { "type", "demo" },
                { "priority", 1 }
            };
            _headersProducer.Send("demo_headers_exchange", message, headers);
            return Ok(new { Message = "消息已发送到头部交换机" });
        }
    }
}